package datastructures;

import java.io.Serializable;

/**
 * An edge connects two words
 * 
 * @author Fredrik
 * 
 */
public class Edge implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 2567055414342564606L;

	private Word leftWord;
	private Word rightWord;

	/**
	 * Default constructor
	 */
	public Edge() {
		// Empty
	}

	/**
	 * Constructing a edge with both words that is connected
	 * 
	 * @param leftWord
	 * @param rightWord
	 */
	public Edge(Word leftWord, Word rightWord) {
		this.leftWord = leftWord;
		this.rightWord = rightWord;
	}

	/**
	 * Returns the one of the words
	 * 
	 * @return a word
	 */
	public Word getLeftWord() {
		return this.leftWord;
	}

	/**
	 * Returns the one of the words
	 * 
	 * @return a word
	 */
	public Word getRightWord() {
		return this.rightWord;
	}

	/**
	 * Sets the left word
	 * 
	 * @param leftWord
	 */
	public void setLeftWord(Word leftWord) {
		this.leftWord = leftWord;
	}

	/**
	 * sets the right word
	 * 
	 * @param rightWord
	 */
	public void setRightWord(Word rightWord) {
		this.rightWord = rightWord;
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object o) {
		if (o instanceof Edge) {
			return equals((Edge) o);
		}
		return false;

	}

	/**
	 * equals returns true only if the edge is considered to be exactly equal to
	 * this one
	 * 
	 * @param e
	 * @return if the edge e is equal to this edge
	 */
	public boolean equals(Edge e) {
		return (this.leftWord.getWordText().equals(e.getLeftWord().getWordText()) && this.rightWord
				.getWordText().equals(e.getRightWord().getWordText()));
	}

	/**
	 * Calculates if this edge is visible
	 * 
	 * @return true if this edge is visible
	 */
	public boolean isVisible() {
		return getLeftWord().isTaken() || getRightWord().isTaken();
	}
	


	/**
	 * delete a word from this edge
	 * 
	 * @param word
	 * @return true if a word was successfully deleted
	 */
	public boolean delete(Word word) {
		if (word.equals(getLeftWord())) {
			return getRightWord().deleteEdge(this);
		} else if (word.equals(getRightWord())) {
			return getLeftWord().deleteEdge(this);
		} else {
			return false;
		}

	}

}
